package blog.controller;

import java.util.Stack;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Displayable;

import blog.SysRuntime;
import blog.gui.BlogMainViewGui;

public class GuiManager {
	private Stack history;
	private Displayable currentScreen;
	
	private static GuiManager _instance;
	
	public static synchronized GuiManager getInstance()
	{
		if ( _instance==null )
		{
			_instance = new GuiManager();
		}
		return _instance;
	}
	
	private GuiManager()
	{
		history = new Stack();
	}
	
	public void back()
	{
		if ( history.size()>0 )
		{
			currentScreen = (Displayable) history.pop();
			if ( currentScreen==BlogMainViewGui.getInstance() )
			{
				BlogMainViewGui.getInstance().reloadData();
			}
			SysRuntime.display.setCurrent( currentScreen );
		}
		else
		{
			//back to MainGui
			BlogMainViewGui.getInstance().reloadData();
			currentScreen = BlogMainViewGui.getInstance();
			SysRuntime.display.setCurrent( currentScreen );
		}
	}
	
	public void show( Displayable nextScreen )
	{
		show( nextScreen, true );
	}
	
	public void show( Displayable nextScreen, boolean bSave )
	{
		if ( bSave && currentScreen!=null )
		{
			this.history.push( this.currentScreen );
		}
		this.currentScreen = nextScreen;
		SysRuntime.display.setCurrent(this.currentScreen);
	}
	
	public void showAlert( Alert alert, boolean bKeepCurrent )
	{
		if ( !bKeepCurrent )
		{
			currentScreen = (Displayable) this.history.pop();
		}
		SysRuntime.display.setCurrent(alert, currentScreen);
	}
	
	public Displayable getCurScreen()
	{
		return this.currentScreen;
	}
	
	public Displayable peek()
	{
		return (Displayable)this.history.peek();
	}
	
	public Displayable pop()
	{
		return (Displayable)history.pop();
	}
}
